package org.nobject.common.swing.g;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextField;

import org.nobject.common.lang.StringUtils;

/**
 * GTextField
 *
 * @author bianrongjun
 * @version 1.0
 */
public class GTextField extends JTextField  implements IG{

	/**
	 * 构造函数 
	 */
	public GTextField() {
		super();
		this.core=new GComponent();
	}

	/**
	 * 构造函数 
	 * @param container
	 * @param name
	 * @param text
	 * @param preferredSize
	 */
	public GTextField(Container container,String name,String text,Dimension preferredSize){
		this();
		if(name!=null) this.setName(name);
		if(text!=null) this.setText(text);
		
		if(preferredSize==null) preferredSize=new Dimension(-1,-1);
		if(preferredSize.width<=0) preferredSize.width=100;
		if(preferredSize.height<=0) preferredSize.height=20;
		this.setPreferredSize(preferredSize);
		if(container!=null) container.add(this);
		this.addKeyListener(new KeyListener() {
			/* (non-Javadoc)
			 * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
			 */
			public void keyTyped(KeyEvent e) {
				
			}
			/* (non-Javadoc)
			 * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
			 */
			public void keyReleased(KeyEvent e) {
				
			}
			/* (non-Javadoc)
			 * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
			 */
			public void keyPressed(KeyEvent e) {
				if(e.isControlDown()){
					if(e.getKeyCode()==KeyEvent.VK_L||e.getKeyCode()==KeyEvent.VK_U){
						GTextField gtf=((GTextField)e.getSource());
						String select=gtf.getSelectedText();
						if(StringUtils.isEmpty(select)) return;
						try {
							String pre=gtf.getText(0, gtf.getSelectionStart());
							String end=gtf.getSelectionEnd()>=gtf.getText().length()?"":gtf.getText(gtf.getSelectionEnd(), gtf.getText().length()-gtf.getSelectionEnd());
							if(e.getKeyCode()==KeyEvent.VK_L){
								gtf.setText(pre+select.toLowerCase()+end);
							}else{
								gtf.setText(pre+select.toUpperCase()+end);
							}
						} catch (Exception e2) {
							e2.printStackTrace();
						}
						
					}
				}
			}
		});
	}
	
	/**
	 * 构造函数 
	 * @param container
	 * @param name
	 * @param text
	 */
	public GTextField(Container container,String name,String text){
		this(container, name, text,null);
	}
	
	/**
	 * 构造函数 
	 * @param container
	 * @param name
	 */
	public GTextField(Container container,String name){
		this(container, name,null,null);
	}
	
	/**
	 * 构造函数 
	 * @param container
	 * @param name
	 * @param preferredSize
	 */
	public GTextField(Container container,String name,Dimension preferredSize){
		this(container, name,null,preferredSize);
	}
	
	/**
	 * 构造函数 
	 * @param preferredSize
	 */
	public GTextField(Dimension preferredSize){
		this(null, null,null,preferredSize);
	}
	
	/**
	 * 构造函数 
	 * @param container
	 * @param preferredSize
	 */
	public GTextField(Container container,Dimension preferredSize){
		this(container, null,null,preferredSize);
	}
	
	/**
	 * 构造函数 
	 * @param container
	 * @param preferredSize
	 * @param text
	 */
	public GTextField(Container container,Dimension preferredSize,String text){
		this(container, null,text,preferredSize);
	}
	
	/**
	 * 构造函数 
	 * @param preferredSize
	 * @param text
	 */
	public GTextField(Dimension preferredSize,String text){
		this(null, null,text,preferredSize);
	}
	
	/** IG */
	private GComponent core;
	public GComponent getGCore() {return core;}
	
	
	public void paintChildren(Graphics g) {core.paintChildren(g);super.paintChildren(g);}
	
}
